package com.tian.web.admin.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.oscache.util.StringUtil;
import com.tian.core.action.JqGridBaseAction;
import com.tian.core.bean.Condition;
import com.tian.core.bean.ConditionOperate;
import com.tian.core.bean.ConditionType;
import com.tian.core.bean.Pages;
import com.tian.core.entity.Group;
import com.tian.core.entity.Master;
import com.tian.core.entity.Mastergroup;
import com.tian.core.entity.Operbutton;
import com.tian.core.entity.Ztree;
import com.tian.core.entity.Ztreegroup;
import com.tian.web.admin.service.GroupService;
import com.tian.web.admin.service.OperbuttonService;
import com.tian.web.admin.service.ZtreeGroupService;
import com.tian.web.admin.service.ZtreeService;

/**
 * 1.用户权限分配--分组，2.组权限分配，3.操作权限分配
 * @author Tian
 *
 */
@Namespace("/admin")
@Results( {  
	@Result(type = "dispatcher",name="power",location="/pages/admin/power_list.jsp"),
	@Result(type = "dispatcher",name="power_menu",location="/pages/admin/power_menu.jsp"),
	@Result(type = "dispatcher",name="power_oper",location="/pages/admin/power_oper.jsp"),
})
public class PowerAction extends JqGridBaseAction {

	private static final long serialVersionUID = 8405514785471817316L;

	private List<Group> gridModel;
	private List<Ztree> ztreeList;
	private Group group;
	private String menuIds;
	private String menu;
	private String id;
	@Resource
	private GroupService groupService;
	@Resource 
	private ZtreeService ztreeService;
	@Resource
	private ZtreeGroupService zgService;
	@Resource
	private OperbuttonService pbService;
	@Override
	public String refreshGridModel() {
		this.record = groupService.getTotalCount(Group.class);
		Pages pages = new Pages();		
    	pages.setPage(page);
    	pages.setTotalNum(record);
    	pages.setPerPageNum(rows);
    	pages.executeCount();
    	total = pages.getAllPage();
    	
    	if(!StringUtil.isEmpty(this.sidx) && !StringUtil.isEmpty(this.sord)){
    		Condition condition1 = new Condition();
        	condition1.setName(this.sidx);
    		//condition1.setValue(Condition.DESC);
    		condition1.setValue(this.sord);
    		condition1.setType(ConditionType.ORDER);
    		conditions.add(condition1);
    	}
    	//只实现equal
    	if(this.search && !StringUtil.isEmpty(this.searchField)
    			&& !StringUtil.isEmpty(this.searchOper)&& !StringUtil.isEmpty(this.searchString)){
    		Condition condition2 = new Condition();
    		condition2.setName(this.searchField);
    		//condition1.setValue(Condition.DESC);
    		condition2.setValue(this.searchString);
    		condition2.setType(ConditionType.PROPERTY);
    		condition2.setOperate(ConditionOperate.EQUAL);
    		conditions.add(condition2);
    	}
		
    	pageList = groupService.findByPages(conditions, pages);
		pages = pageList.getPages();
		
        this.rows = pages.getPerPageNum();
        this.record = pages.getTotalNum();
        this.page = pages.getPage();
        
        gridModel = (List<Group>)pageList.getObjectList();

		System.out.println("rows:"+rows+" page:"+ page + " total:" + total + " record:" + record);
		return SUCCESS;
	}
	
	@Override
	public String execute() throws Exception {
		return "power";
	}
	
	public String list() throws Exception {
		
		return this.refreshGridModel();
	}
	
	public String powerMenu() throws Exception {
    	ztreeList = ztreeService.getAll(Ztree.class);
		
		List<Ztree> list = new ArrayList<Ztree>();

		Group g = groupService.get(group.getId());
		group.setGroupinfo(g.getGroupinfo());
		Set<Ztreegroup> zgs = g.getZtreegroups();
		for (Ztreegroup zg : zgs) {
			Ztree ztree = zg.getZtree();
			ztree.setZtreegroups(null);
			ztree.setOperbuttons(null);
			list.add(ztree);
		}
		//设置选中菜单
		for(Ztree tree : ztreeList){
			tree.setZtreegroups(null);
			tree.setOperbuttons(null);
			tree.setUrl("");
			for(Ztree node : list){
				if(node.getId().equals(tree.getId())){
					tree.setChecked(true);
				}
			}
		}
		Collections.sort(ztreeList);
    	menu = JSONArray.fromObject(ztreeList).toString();
    	return "power_menu";
    }
	
	public String powerMenuSave() throws Exception{
		String[] ids;
		Ztreegroup zg = new Ztreegroup();
		
		if(!StringUtil.isEmpty(menuIds)){
			//删除原有的菜单
			//List<Ztreegroup> zgList = zgService.getList("group.id", group.getId());
			zgService.deleteByProperty(group.getId());
			
			ids = menuIds.split(",");
			for(int i=0;i<ids.length;i++){
				Ztree tree = ztreeService.get(Integer.parseInt(ids[i]));
				zg.setGroup(group);
				zg.setZtree(tree);
				zg.setCreatedate(new Date());
				zgService.save(zg);
			}
		}
		
		return this.execute();
	}
	
	public String powerOper() throws Exception{
		Group g = groupService.get(group.getId());
		group.setGroupinfo(g.getGroupinfo());
		
		return "power_oper";
	}
	
	public String getOperbuttons() throws Exception{
		if(StringUtil.isEmpty(this.id)){
    		return ERROR;
    	}
		List<Operbutton> buttons = pbService.getList("tree.id",Integer.parseInt(id));
		return ajax(buttons);
	}
	
	public String powerOperSave() throws Exception{
		
		return this.execute();
	}

	public List<Group> getGridModel() {
		return gridModel;
	}

	public void setGridModel(List<Group> gridModel) {
		this.gridModel = gridModel;
	}

	public List<Ztree> getZtreeList() {
		return ztreeList;
	}

	public void setZtreeList(List<Ztree> ztreeList) {
		this.ztreeList = ztreeList;
	}

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public String getMenuIds() {
		return menuIds;
	}

	public void setMenuIds(String menuIds) {
		this.menuIds = menuIds;
	}

	public String getMenu() {
		return menu;
	}

	public void setMenu(String menu) {
		this.menu = menu;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

}
